package com.zhu.utils;

import com.zhu.config.RedisConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * redis 链接工具类
 */
public class RedisUtil {

    private static JedisPool jedisPool;

    private static void getJedisPool(){

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(100);  //最大链接数
        jedisPoolConfig.setMaxIdle(5);  //
        jedisPoolConfig.setMinIdle(3);
        jedisPoolConfig.setBlockWhenExhausted(true);  //重试
        jedisPoolConfig.setMaxWaitMillis(2000);  //最大链接等待时间
        jedisPoolConfig.setTestOnBorrow(true);

        jedisPool = new JedisPool(jedisPoolConfig, RedisConfig.REDIS_CONNECT_HOST, RedisConfig.REDIS_CONNECT_PORT, 10000);

    }

    public static Jedis getJedis(){
        if(jedisPool == null){
            getJedisPool();
        }
        //get jedis
        return jedisPool.getResource();
    }

    /*
    public static void main(String[] args) {

    }
     */

}
